package com.tanhua.api.impl.mongo;

import com.tanhua.api.mongo.VisitorsApi;
import com.tanhua.domain.mongo.Visitors;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class VisitorsApiImpl implements VisitorsApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public List<Visitors> findvisitorsList(long time, Long userId) {

        Query query = new Query(Criteria.where("userId").is(userId).and("date").gt(time));
        query.with(new Sort(Sort.Direction.DESC,"date"));
        query.skip(0).limit(6);
        List<Visitors> visitorsList = mongoTemplate.find(query, Visitors.class);
        return visitorsList;
    }

    @Override
    public PageBeanVo findVisitorsList(Integer page, Integer pagesize, Long id) {
        Query query = new Query(Criteria.where("userId").is(id));
        query.with(new Sort(Sort.Direction.DESC,"date"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Visitors> visitorsList = mongoTemplate.find(query, Visitors.class);
        long count = mongoTemplate.count(query, Visitors.class);
        return new PageBeanVo(page,pagesize,count,visitorsList);
    }
}
